
Page({

  data: {
    tempImgFilePaths: [],
    tempVideoFilePaths: [],
    tempSoundFilePaths: [],
    tempFilePaths: [],
    playVideo: false,
    src: '',
    saveCount: 0,
    addFileHeight: 0
    //以下为上传图片至云存储
    //images_fileID: [],
  },
  // 整体高度
  addFileHeightMethod() {
    let that = this;
    wx.getSystemInfo({
      success: function (res) {
        // debugger
        var clientHeight = res.windowHeight * 2
        // debugger
        that.setData({
          addFileHeight: clientHeight + "rpx"
        });
      }
    });
  },
  onLoad: function (options) {
    // 整体内容高度
    this.addFileHeightMethod();
    this.setData({ saveCount: 0 });
    console.log("addfile", options)
    options = JSON.parse(options.model);
    this.videoContext = wx.createVideoContext('prew_video');
    if ((options.tempImgFilePaths == null) || (options.tempImgFilePaths == undefined)) {
      this.setData({
        tempImgFilePaths: []
      });
    } else {
      this.setData({
        tempImgFilePaths: options.tempImgFilePaths
      });
    }
    if ((options.tempVideoFilePaths == null) || (options.tempVideoFilePaths == undefined)) {
      this.setData({
        tempVideoFilePaths: []
      });
    } else {
      this.setData({
        tempVideoFilePaths: options.tempVideoFilePaths
      });
    }

    if ((options.tempSoundFilePaths == null) || (options.tempSoundFilePaths == undefined)) {
      this.setData({
        tempSoundFilePaths: []
      });
    } else {
      this.setData({
        tempSoundFilePaths: options.tempSoundFilePaths
      });
    }
  },
  //离开当前页面
  onUnload() {
    this.setData({ saveCount: this.data.saveCount + 1 });
    if (this.data.saveCount > 1) return;
    //直接调用上一个页面的setData()方法，把数据存到上一个页面中去
    var pages = getCurrentPages();
    var currPage = pages[pages.length - 1]; //当前页面
    var prevPage = pages[pages.length - 2];
    let model = {
      tempImgFilePaths: this.data.tempImgFilePaths,
      tempVideoFilePaths: this.data.tempVideoFilePaths,
      tempSoundFilePaths: this.data.tempSoundFilePaths
    }
    prevPage.changeFileData(model);
  },
  //图片
  //选择图片
  ChooseImg: function () {
    let that = this;
    wx.chooseImage({
      count: 9, // 默认最多9张图片，可自行更改
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: res => {

        let tempImgFilePaths = this.data.tempImgFilePaths;
        res.tempFiles.forEach(x => {
          tempImgFilePaths.push(x);
        })
        that.setData({
          tempImgFilePaths: tempImgFilePaths
        })
      }
    });
    // debugger
    console.log(this.data.tempImgFilePaths)
  },
  //预览图片
  PreviewImg: function (e) {
    // debugger
    let index = e.target.dataset.index;
    let that = this;
    wx.previewImage({
      current: that.data.tempImgFilePaths[index],
      urls: that.data.tempImgFilePaths,
    })
  },
  //长按删除图片
  DeleteImg: function (e) {
    var that = this;
    var tempImgFilePaths = that.data.tempImgFilePaths;
    var index = e.currentTarget.dataset.index; //获取当前长按图片下标
    wx.showModal({
      title: '提示',
      content: '确定要删除此图片吗？',
      success: function (res) {
        if (res.confirm) {
          tempImgFilePaths.splice(index, 1);
        } else if (res.cancel) {
          return false;
        }
        that.setData({
          tempImgFilePaths
        });
      }
    });
  },

  UploadBtntap: function (e) {
 
    this.setData({ saveCount: this.data.saveCount + 1 });
    if (this.data.saveCount > 1) return;
    //直接调用上一个页面的setData()方法，把数据存到上一个页面中去
    var pages = getCurrentPages();
    var currPage = pages[pages.length - 1]; //当前页面
    var prevPage = pages[pages.length - 2];
    let model = {
      tempImgFilePaths: this.data.tempImgFilePaths,
      tempVideoFilePaths: this.data.tempVideoFilePaths,
      tempSoundFilePaths: this.data.tempSoundFilePaths
    }
    prevPage.changeFileData(model);
    wx.navigateBack({
      delta: 1,
    })
  }
  // 上传图片至云数据库,可自行参考
  /*
  UploadBtntap: function (e) {
    var count = 0;
    var h = this.data.tempFilePaths.length;
    if (h != 0) {
      this.setData({
        images_fileID: []
      })
    }
    for (var i = 0; i < h; i++) {
      //上传文件
      var imageUrl = this.data.tempFilePaths[i].split("/");
      var name = imageUrl[imageUrl.length - 1];
      var images_fileID = this.data.images_fileID;
      wx.cloud.uploadFile({
        cloudPath: name,     //自定义云存储路径
        filePath: this.data.tempFilePaths[i],
        success: res => {
          images_fileID.push(res.fileID);
          this.setData({
            images_fileID: images_fileID         //更新data中的 fileID
          })
          fail: res => {
            count++;
            wx.hideToast();
            wx.showModal({
              title: '错误提示',
              content: '上传图片失败',
              showCancel: false,
              success: function (res) { }
            })
          }
        }
      });
    }
  }
  */
})